// pages/upgrade/upgrade.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        siteUrl     : app.globalData.siteUrl,
        index       : 1,
        tupian      : getApp().globalData.imgUrl,
        show_rules  : true,
        show_success: true,
    },

    /**关闭规则*/
    closeRules() {
        this.setData({show_rules: true})
    },
    /**获取升级规则*/
    getRules: function (e) {
        let self       = this;
        let logo       = e.currentTarget.dataset.logo;
        let level      = e.currentTarget.dataset.level;
        let name       = e.currentTarget.dataset.name;
        let subject_id = e.currentTarget.dataset.id;
        let userInfo   = app.globalData.userInfo;
        let rules_url  = 'knowledge/updateRules';
        app.getdata.fetchApi(rules_url, {
            session_key: userInfo.token,
            level      : level,
            subject_id : subject_id
        }).then(res => {
            if (res.status === 1) {
                self.setData({
                    show_rules: false,
                    total     : res.data,
                    name      : name,
                    subject_id: subject_id,
                    logo      : logo
                });
            }
        }).catch(err => {
            console.error(err);
        });

    },

    /**升级科目*/
    updateSub(e) {
        let self       = this;
        let userInfo   = app.globalData.userInfo;
        let update_url = 'knowledge/updateSubject';
        let level      = e.currentTarget.dataset.level;
        let subject_id = self.data.subject_id;
        app.getdata.fetchApi(update_url, {
            session_key: userInfo.token,
            level      : level,
            subject_id : subject_id
        }).then(res => {
            if (res.status === 1) {
                app.show_success()
                self.setData({
                    show_rules  : true,
                    show_success: false,
                });
            } else {
                wx.showModal({
                    title     : '提示',
                    content   : res.message,
                    showCancel: false,
                })
            }
        }).catch(err => {
            console.error(err);
        });
    },

    /**关闭成功提示*/
    closeSuccess: function () {
        this.getUserSubGrad();
        this.setData({
            show_success: true,
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getUserSubGrad();
    },

    getUserSubGrad() {
        let self     = this;
        let list_url = 'knowledge/getSubjectList';
        let userInfo = app.globalData.userInfo;
        app.getdata.fetchApi(list_url, {session_key: userInfo.token}).then(res => {
            self.setData({list: res.data});
        }).catch(err => {
            this.setData({
                subtitle: err.message,
                loading : false
            })

        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
});